﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml;
using com.lover.astd.common.manager;

namespace com.lover.astd.common.logic
{
    public class MgrBase
    {
        protected Color _logColor = System.Drawing.Color.Black;
        protected void logInfo(ILogger logger, string text) { logger.log(text, _logColor); }
        protected long immediate()
        {
            return 2000;
        }
        protected long an_hour_later()
        {
            return 60*60*1000;
        }
        /// <summary>
        /// 半小时执行一次
        /// </summary>
        /// <returns></returns>
        protected long next_halfhour()
        {
            // 以下几个函数除了forever外都需要算出具体的时间戳
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset = 0;
            if (dtnow.Minute < 30) offset = 30 - dtnow.Minute;
            else offset = 60 - dtnow.Minute;
            return offset * 60 * 1000;
        }
        /// <summary>
        /// 1小时执行一次
        /// </summary>
        /// <returns></returns>
        protected long next_hour()
        {
            // 以下几个函数除了forever外都需要算出具体的时间戳
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset = 60 - dtnow.Minute;
            return offset * 60 * 1000;
        }
        protected long next_day()
        {
            // 5:30
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset_minute = 0, offset_hour = 0;
            if (dtnow.Hour < 5) offset_hour = 5 - dtnow.Hour - 1;
            else offset_hour = 24 + 5 - dtnow.Hour - 1;
            offset_minute = 60 - dtnow.Minute;
            return (offset_hour * 60 * 60 + (offset_minute + 30) * 60) * 1000;
        }
        protected long next_dinner()
        {
            //10:01 19:01
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset_hour = 0, offset_minute = 0;
            if (dtnow.Hour < 10) offset_hour = 10 - dtnow.Hour - 1;
            else if (dtnow.Hour < 19) offset_hour = 19 - dtnow.Hour - 1;
            else offset_hour = 24 + 10 - dtnow.Hour - 1;
            offset_minute = 60 - dtnow.Minute;
            return (offset_hour * 60 * 60 + (offset_minute + 1) * 60) * 1000;
        }
        protected bool in_gongjian_time()
        {
            // 10:00-10:30 / 15:00-15:30 / 20:30-21:00
            DateTime dtnow = TimeMgr.DateTimeNow;
            int hour = dtnow.Hour, minute = dtnow.Minute;
            if (hour == 10 && minute < 30) return true;
            if (hour == 15 && minute < 30) return true;
            if (hour == 20 && minute > 30) return true;
            return false;
        }
        protected long smaller_time(long time1, long time2)
        {
            if (time1 < time2) return time1;
            else return time2;
        }
        protected string formatTime(long milliseconds)
        {
            long minutes = milliseconds / (60 * 1000);
            long seconds = milliseconds / 1000 - minutes * 60;
            string ret = String.Format("{0:d2}:{1:d2}", minutes, seconds); ;
            return ret;
        }
        protected void tryUseCard(ProtocolMgr protocol, ILogger logger, XmlDocument xml)
        {
            XmlNodeList card_nodes = xml.SelectNodes("/results/card");
            if (card_nodes == null) return;
            foreach (XmlNode card_node in card_nodes)
            {
                if (card_node == null || !card_node.HasChildNodes) continue;
                int cardtype = -1, cardnum = 0, activated = 0; string cardname = "";
                XmlNode cardtype_node = card_node.SelectSingleNode("cardtype");
                if (cardtype_node != null) Int32.TryParse(cardtype_node.InnerText, out cardtype);

                XmlNode cardnum_node = card_node.SelectSingleNode("cardnum");
                if (cardnum_node != null) Int32.TryParse(cardnum_node.InnerText, out cardnum);

                XmlNode activated_node = card_node.SelectSingleNode("active");
                if (activated_node != null) Int32.TryParse(activated_node.InnerText, out activated);

                XmlNode cardname_node = card_node.SelectSingleNode("cardname");
                if (cardname_node != null) cardname = cardname_node.InnerText;

                if (cardtype >= 0 && cardnum > 0 && activated == 0) useCardByType(protocol, logger, cardtype);
            }
        }
        protected void tryCancelCard(ProtocolMgr protocol, ILogger logger, XmlDocument xml)
        {
            XmlNodeList card_nodes = xml.SelectNodes("/results/card");
            if (card_nodes == null) return;
            foreach (XmlNode card_node in card_nodes)
            {
                if (card_node == null || !card_node.HasChildNodes) continue;
                int cardtype = -1, cardnum = 0, activated = 0; string cardname = "";
                XmlNode cardtype_node = card_node.SelectSingleNode("cardtype");
                if (cardtype_node != null) Int32.TryParse(cardtype_node.InnerText, out cardtype);

                XmlNode cardnum_node = card_node.SelectSingleNode("cardnum");
                if (cardnum_node != null) Int32.TryParse(cardnum_node.InnerText, out cardnum);

                XmlNode activated_node = card_node.SelectSingleNode("active");
                if (activated_node != null) Int32.TryParse(activated_node.InnerText, out activated);

                XmlNode cardname_node = card_node.SelectSingleNode("cardname");
                if (cardname_node != null) cardname = cardname_node.InnerText;

                if (cardtype >= 0 && activated != 0) useCardByType(protocol, logger, cardtype);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cardType">1=商人召唤卡, 2=强化暴击卡, 3=强化打折卡, 4=兵器升级卡, 
        /// 5=兵器暴击卡, 6=政绩翻倍卡, 7=征收翻倍卡, 8=纺织翻倍卡</param>
        protected void useCardByType(ProtocolMgr protocol, ILogger logger, int cardType)
        {
            string url = "/root/goods!useCardByTypes.action";
            string data = "cardTypes=" + cardType;
            com.lover.astd.common.model.ServerResult result = protocol.postXml(url, data, "使用或取消卡");
            if (result == null) return;
            if (!result.CmdSucceed) return;
            XmlDocument xml = result.CmdResult;
            string[] names = new string[] { "", "商人召唤卡", "强化暴击卡", "强化打折卡", "兵器升级卡", "兵器暴击卡", "政绩翻倍卡", "征收翻倍卡", "纺织翻倍卡" };
            if (cardType > 0 && cardType < names.Length)
            {
                logInfo(logger, String.Format("使用[{0}]", names[cardType]));
            }
            else
            {
                logInfo(logger, String.Format("使用[卡type={0}]", cardType));
            }
            return;
        }
        protected List<int> generateIds(string id_string)
        {
            List<int> idstrs_to = new List<int>();
            if (id_string == null || id_string == "") return idstrs_to;
            string[] idstrs = id_string.Split(',');
            foreach (string strid in idstrs)
            {
                if (strid == null || strid == "") continue;
                int qhid = 0;
                Int32.TryParse(strid, out qhid);
                if (qhid == 0) continue;
                idstrs_to.Add(qhid);
            }
            return idstrs_to;
        }
    }
}
